package test1;

import java.util.ArrayList;

import parser.*;

public class value {
	public static Double run(ArrayList<Word> word) {
		boolean jump=false;
		int jumpNum=0;
		Double left=0.0;
		Double right=0.0;
		String u="";
		for(int i=0;i<word.size();i++) {
			
			if(jump) {
				jumpNum--;
				if(jumpNum==0) {
					jump=false;
				}
				continue;
			}
			String this_word=word.get(i).text;
			
			if(Tools.math.isNum(this_word)) {
				
				left=Double.parseDouble(this_word);
				System.out.println("发现数字:"+left+",列入左边");
				continue;
			}
			if(this_word.equals("+")) {
				if(u.equals("+")) {
					System.out.print("发现符号:+，数字:"+left+"和数字"+right+"进行运算");
					left=left+right;
					System.out.print(",结果为:"+left+",这个结果被列入左边\n");
				}
				if(u.equals("-")) {
					System.out.print("发现符号:-，数字:"+left+"和数字"+right+"进行运算");
					left=left-right;
					System.out.print(",结果为:"+left+",这个结果被列入左边\n");
				}
				if(word.get(i+1).text.equals("(")) {
					int x=2;
					ArrayList<Word> aaa=new ArrayList<Word>();
					for(;i+x<word.size();x++) {
						if(word.get(i+x).text.equals(")")) {
							aaa.add(new Word(";",cfg.WORD_U,-1));
							break;
						}else {
							aaa.add(word.get(i+x));
						}
					}
					System.out.println("发现符号:(,扫描到括号内表达式,开始递归运算。。。。。。\n");
					right=run(aaa);
					System.out.println("\n递归运算结果:"+right);
					jump=true;
					jumpNum=x;
				}else {
					right=Double.parseDouble(word.get(i+1).text);
					jump=true;
					jumpNum=1;
				}
				u="+";
				System.out.println("发现符号:+，数字:"+right+",列入右边");
				
				continue;
			}
			if(this_word.equals("-")) {
				if(u.equals("-")) {
					System.out.print("发现符号:-，数字:"+left+"和数字"+right+"进行运算");
					left=left-right;
					System.out.print(",结果为:"+left+",这个结果被列入左边\n");
				}
				if(u.equals("+")) {
					System.out.print("发现符号:+，数字:"+left+"和数字"+right+"进行运算");
					left=left+right;
					System.out.print(",结果为:"+left+",这个结果被列入左边\n");
				}
				if(word.get(i+1).text.equals("(")) {
					int x=2;
					ArrayList<Word> aaa=new ArrayList<Word>();
					for(;i+x<word.size();x++) {
						if(word.get(i+x).text.equals(")")) {
							aaa.add(new Word(";",cfg.WORD_U,-1));
							break;
						}else {
							aaa.add(word.get(i+x));
						}
					}
					System.out.println("发现符号:(,扫描到括号内表达式,开始递归运算。。。。。。\n");
					right=run(aaa);
					System.out.println("\n递归运算结果:"+right);
					jump=true;
					jumpNum=x;
				}else {
					right=Double.parseDouble(word.get(i+1).text);
					jump=true;
					jumpNum=1;
				}
				u="-";
				System.out.println("发现符号:-，数字:"+right+",列入右边");
				
				continue;
			}
			if(this_word.equals("*")) {
				Double this_left;
				String this_next;
				if(word.get(i+1).text.equals("(")) {
					int x=2;
					ArrayList<Word> aaa=new ArrayList<Word>();
					for(;i+x<word.size();x++) {
						if(word.get(i+x).text.equals(")")) {
							aaa.add(new Word(";",cfg.WORD_U,-1));
							break;
						}else {
							aaa.add(word.get(i+x));
						}
					}
					System.out.println("发现符号:(,扫描到括号内表达式,开始递归运算。。。。。。\n");
					this_next=String.valueOf(run(aaa));
					System.out.println("\n递归运算结果:"+this_next);
					jump=true;
					jumpNum=x;
				}else {
					this_next=word.get(i+1).text;
					jump=true;
					jumpNum=1;
				}
				if(u.equals("+")||u.equals("-")) {
					this_left=right;
					right=this_left*Double.parseDouble(this_next);
					System.out.println("发现符号:*，基于:+，数字:"+this_left+"与"+Double.parseDouble(this_next)+"做运算,结果为:"+right+",这个结果被列入右边");
				}else {
					this_left=left;
					left=this_left*Double.parseDouble(this_next);
					System.out.println("发现符号:*，数字:"+this_left+"与"+Double.parseDouble(this_next)+"做运算,结果为:"+left+",这个结果被列入左边");
				}
				continue;
			}
			if(this_word.equals("/")) {
				Double this_left;
				String this_next;
				if(word.get(i+1).text.equals("(")) {
					int x=2;
					ArrayList<Word> aaa=new ArrayList<Word>();
					for(;i+x<word.size();x++) {
						if(word.get(i+x).text.equals(")")) {
							aaa.add(new Word(";",cfg.WORD_U,-1));
							break;
						}else {
							aaa.add(word.get(i+x));
						}
					}
					System.out.println("发现符号:(,扫描到括号内表达式:"+aaa+",开始递归运算。。。。。。\n");
					this_next=String.valueOf(run(aaa));
					System.out.println("\n递归运算结果:"+this_next);
					jump=true;
					jumpNum=x;
				}else {
					this_next=word.get(i+1).text;
					jump=true;
					jumpNum=1;
				}
				if(u.equals("+")||u.equals("-")) {
					this_left=right;
					right=this_left/Double.parseDouble(this_next);
					System.out.println("发现符号:/，基于:+，数字:"+this_left+"与"+Double.parseDouble(this_next)+"做运算,结果为:"+right+",这个结果被列入右边");
				}else {
					this_left=left;
					left=this_left/Double.parseDouble(this_next);
					System.out.println("发现符号:/，数字:"+this_left+"与"+Double.parseDouble(this_next)+"做运算,结果为:"+left+",这个结果被列入左边");
				}
				continue;
			}
			if(this_word.equals("(")) {
				int x=1;
				
				ArrayList<Word> aaa=new ArrayList<Word>();
				for(;i+x<word.size();x++) {
					if(word.get(i+x).text.equals(")")) {
						aaa.add(new Word(";",cfg.WORD_U,-1));
						break;
					}else {
						aaa.add(word.get(i+x));
					}
				}
				System.out.println("发现符号:(,扫描到括号内表达式,开始递归运算。。。。。。\n");
				left=run(aaa);
				System.out.println("\n递归运算结果:"+left);
				jump=true;
				jumpNum=x;
				continue;
			}
			if(this_word.equals(";")) {
				if(u.equals("+")) {
					System.out.print("发现符号:;，基于:+，数字:"+left+"与"+right+"做运算,");
					left=left+right;
					System.out.print("结果为:"+left+",这个结果被列入左边\n");
				}
				if(u.equals("-")) {
					System.out.print("发现符号:;，基于:-，数字:"+left+"与"+right+"做运算,");
					left=left-right;
					System.out.print("结果为:"+left+",这个结果被列入左边\n");
				}
				return left;
			}
			
		}
		return 0.0;
	}
}
